﻿using System.Collections;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp61.Models;

namespace WpfApp61
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //使用View对象的过滤、排序和分组是一种愚蠢的操作，他会极大的增加软件的运行负担，不建议使用。
        private ListCollectionView view = null!;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ObservableCollection<Person> people = new() {
                new Person(){
                    Id=0,
                    Name="张三",
                    Gerder="F",
                    Age=18
                },
                new Person(){
                    Id=1,
                    Name="李四",
                    Gerder="F",
                    Age=19
                },
                new Person(){
                    Id=2,
                    Name="王五",
                    Gerder="F",
                    Age=20
                },
                new Person(){
                    Id=4,
                    Name="林黛玉",
                    Gerder="M",
                    Age=18
                },
                new Person(){
                    Id=3,
                    Name="凤姐",
                    Gerder="M",
                    Age=50
                },
                new Person(){
                    Id=5,
                    Name="王二麻子",
                    Gerder="F",
                    Age=51
                },
            };

            //使用传统的方式进行过滤
            //IEnumerable<Person> p = people.Where(m => m.Gerder == "M");   
            //LbPeople.ItemsSource = p;


            //***如下使用新的方式 view.Filter
            LbPeople.ItemsSource = people;

            view = (ListCollectionView)CollectionViewSource.GetDefaultView(LbPeople.ItemsSource);

            //1、过滤（筛选，或者选择）
            //直接使用View对象的过滤操作
            //view.Filter = m => ((Person)m).Gerder == "F";

            //2、排序  按照性别分组+按照名字排序
            view.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name", System.ComponentModel.ListSortDirection.Ascending));  //按照名字排序
            view.GroupDescriptions.Add(new PropertyGroupDescription("Gerder"));  //按照性别分组
        
        
        }

        public class NameSortByLenghtComparer : IComparer
        {
            public int Compare(object x, object y)
            {
                string name1 = ((Person)x).Name;
                string name2 = ((Person)y).Name;

                return name1.Length.CompareTo(name2.Length);
            }
        }
    }
}